﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Windows.Data;
using System.ComponentModel;
using EIA.PM.Model;

namespace EIA.SLClient.ProjectManage.Members
{
    public class MembersViewModel : SimpleMvvmToolkit.ViewModelBase<MembersViewModel>
    {
        public MembersViewModel()
        {
            this.RegisterToReceiveMessages<RProjectMember>(InnerMessageTokens.NEW_CANCEL, EP_NewCancel);
        }
        private void EP_NewCancel(object sender, SimpleMvvmToolkit.NotificationEventArgs<RProjectMember> e)
        {
            if (MemberCollection != null)
            {
                MemberCollection.Remove(e.Data);
            }
        }

        private void Busy(string msg = "等待...")
        {
            IsBusy = true;
            SendMessage<bool>(EIA.SLClient.ProjectManage.Infrastructure.MessageTokens.BUSY_STATE, new SimpleMvvmToolkit.NotificationEventArgs<bool>(msg, true));
        }

        private void Free()
        {
            IsBusy = false;
            SendMessage<bool>(EIA.SLClient.ProjectManage.Infrastructure.MessageTokens.BUSY_STATE, new SimpleMvvmToolkit.NotificationEventArgs<bool>("", false));
        }

        #region notifyprop

        protected bool _IsBusy;
        /// <summary>
        /// 
        /// </summary>
        public bool IsBusy
        {
            get { return _IsBusy; }
            set
            {
                _IsBusy = value;
                NotifyPropertyChanged(m => m.IsBusy);
            }
        }

        protected string _WaitMessage;
        /// <summary>
        /// 等待消息
        /// </summary>
        public string WaitMessage
        {
            get { return _WaitMessage; }
            set
            {
                _WaitMessage = value;
                NotifyPropertyChanged(m => m.WaitMessage);
            }
        }

        protected EIA.PM.Model.TProjectBasic _ProjectBasic;
        /// <summary>
        /// 注释
        /// </summary>
        public EIA.PM.Model.TProjectBasic ProjectBasic
        {
            get { return _ProjectBasic; }
            set
            {
                _ProjectBasic = value;
                NotifyPropertyChanged(m => m.ProjectBasic);
            }
        }

        protected System.Collections.ObjectModel.ObservableCollection<EIA.PM.Model.RProjectMember> _MemberCollection;
        /// <summary>
        /// 注释
        /// </summary>
        public System.Collections.ObjectModel.ObservableCollection<EIA.PM.Model.RProjectMember> MemberCollection
        {
            get { return _MemberCollection; }
            set
            {
                _MemberCollection = value;
                NotifyPropertyChanged(m => m.MemberCollection);
            }
        }

        #endregion

        #region command

        #region RetrieveItemsCommand

        private SimpleMvvmToolkit.DelegateCommand _RetrieveItemsCommand = null;
        public SimpleMvvmToolkit.DelegateCommand RetrieveItemsCommand
        {
            get
            {
                if (_RetrieveItemsCommand == null)
                {
                    _RetrieveItemsCommand = new SimpleMvvmToolkit.DelegateCommand(RetrieveItems, CanExcuteRetrieveItems);
                } return _RetrieveItemsCommand;
            }
            set { _RetrieveItemsCommand = value; NotifyPropertyChanged(m => m.RetrieveItemsCommand); }
        }

        public void RetrieveItems()
        {
            EIA.PM.BLL.ClientProxy.RProjectMemberProxy proxy = new EIA.PM.BLL.ClientProxy.RProjectMemberProxy();
            Busy();
            Dictionary<string, object> con = new Dictionary<string, object>();
            con[EIA.PM.Model.RProjectMember.PRJ_ID] = ProjectBasic.PrjId;
            proxy.List(con, int.MaxValue, 1, (lst) =>
            {
                Free();
                MemberCollection = new System.Collections.ObjectModel.ObservableCollection<PM.Model.RProjectMember>(lst);
            }, (ex) =>
            {
                Free();
                System.Windows.MessageBox.Show("获取项目成员发生错误！", "提示", System.Windows.MessageBoxButton.OK);
            });
        }

        public bool CanExcuteRetrieveItems()
        {
            return true;
        }

        #endregion

        #region InsertItemCommand

        private SimpleMvvmToolkit.DelegateCommand _InsertItemCommand = null;
        public SimpleMvvmToolkit.DelegateCommand InsertItemCommand
        {
            get
            {
                if (_InsertItemCommand == null)
                {
                    _InsertItemCommand = new SimpleMvvmToolkit.DelegateCommand(InsertItem, CanExcuteInsertItem);
                } return _InsertItemCommand;
            }
            set { _InsertItemCommand = value; NotifyPropertyChanged(m => m.InsertItemCommand); }
        }

        public void InsertItem()
        {
            RProjectMember pm = new RProjectMember() { PrjId = ProjectBasic.PrjId };
            var t = MemberCollection == null ? 0 : MemberCollection.Sum((m) => { return m == null ? 0 : m.WorkRate; });
            pm.WorkRate = 1 - t;
            if (MemberCollection == null)
            {
                MemberCollection = new System.Collections.ObjectModel.ObservableCollection<RProjectMember>();
            }
            MemberCollection.Insert(0, pm);
            this.SendMessage<double>(InnerMessageTokens.WORK_RATE_MAXVALUE, new SimpleMvvmToolkit.NotificationEventArgs<double>("", pm.WorkRate));
            this.SendMessage<RProjectMember>(InnerMessageTokens.NEW, new SimpleMvvmToolkit.NotificationEventArgs<RProjectMember>("", pm));
        }

        public bool CanExcuteInsertItem()
        {
            return true;
        }

        #endregion

        #region UpdateItemCommand

        private SimpleMvvmToolkit.DelegateCommand<EIA.PM.Model.RProjectMember> _UpdateItemCommand = null;
        public SimpleMvvmToolkit.DelegateCommand<EIA.PM.Model.RProjectMember> UpdateItemCommand
        {
            get
            {
                if (_UpdateItemCommand == null)
                {
                    _UpdateItemCommand = new SimpleMvvmToolkit.DelegateCommand<EIA.PM.Model.RProjectMember>(UpdateItem, CanExcuteUpdateItem);
                } return _UpdateItemCommand;
            }
            set { _UpdateItemCommand = value; NotifyPropertyChanged(m => m.UpdateItemCommand); }
        }

        public void UpdateItem(EIA.PM.Model.RProjectMember p)
        {
            var t = MemberCollection.Sum((m) => { return m == null ? 0 : m.WorkRate; });
            this.SendMessage<double>(InnerMessageTokens.WORK_RATE_MAXVALUE, new SimpleMvvmToolkit.NotificationEventArgs<double>("", 1 - t));
            this.SendMessage<RProjectMember>(InnerMessageTokens.UPDATE, new SimpleMvvmToolkit.NotificationEventArgs<RProjectMember>("", p));
        }

        public bool CanExcuteUpdateItem(EIA.PM.Model.RProjectMember p)
        {
            return true;
        }

        #endregion

        #region DeleteItemCommand

        private SimpleMvvmToolkit.DelegateCommand<EIA.PM.Model.RProjectMember> _DeleteItemCommand = null;
        public SimpleMvvmToolkit.DelegateCommand<EIA.PM.Model.RProjectMember> DeleteItemCommand
        {
            get
            {
                if (_DeleteItemCommand == null)
                {
                    _DeleteItemCommand = new SimpleMvvmToolkit.DelegateCommand<EIA.PM.Model.RProjectMember>(DeleteItem, CanExcuteDeleteItem);
                } return _DeleteItemCommand;
            }
            set { _DeleteItemCommand = value; NotifyPropertyChanged(m => m.DeleteItemCommand); }
        }

        public void DeleteItem(EIA.PM.Model.RProjectMember p)
        {
            if (System.Windows.MessageBox.Show(string.Format("确认删除成员 [{0}]？", p.StfName), "提示", System.Windows.MessageBoxButton.OKCancel) != System.Windows.MessageBoxResult.OK) return;

            EIA.PM.BLL.ClientProxy.RProjectMemberProxy proxy = new EIA.PM.BLL.ClientProxy.RProjectMemberProxy();
            Busy();
            var lst = new List<RProjectMember>();
            lst.Add(p);
            proxy.DeleteList(lst, (r) =>
            {

                Free();
                if (r)
                {
                    MemberCollection.Remove(p);
                }
                else
                {
                    System.Windows.MessageBox.Show("删除成员失败！", "提示", System.Windows.MessageBoxButton.OK);
                }

            }, (ex) =>
            {
                Free();
                System.Windows.MessageBox.Show("删除成员发生错误！", "提示", System.Windows.MessageBoxButton.OK);
            });
        }

        public bool CanExcuteDeleteItem(EIA.PM.Model.RProjectMember p)
        {
            return true;
        }

        #endregion

        #endregion

        public void OnLoad()
        {
            this.SendMessage<object, EIA.PM.Model.TProjectBasic>(EIA.SLClient.ProjectManage.Infrastructure.MessageTokens.PROJECT_CURRENT, new SimpleMvvmToolkit.NotificationEventArgs<object, PM.Model.TProjectBasic>("", null, (p) =>
            {
                if (ProjectBasic != p)
                {
                    ProjectBasic = p;
                    MemberCollection = null;
                    RetrieveItems();
                }
            }));
        }
    }
}